package com.czl.elm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czl.elm.dao.ActivityMapper;
import com.czl.elm.dao.RedParperMapper;
import com.czl.elm.dao.ShopMapper;
import com.czl.elm.domain.Activity;
import com.czl.elm.domain.RedParper;
import com.czl.elm.domain.RedShopParper;
import com.czl.elm.domain.UsersLoginParam;
import com.czl.elm.service.IActivityService;
import com.czl.elm.util.ActionResult;
import com.czl.elm.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zuoleiChen
 * @since 2021-06-15
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private RedParperMapper redParperMapper;

    @Autowired
    private ShopMapper shopMapper;
    @Override
    public ActionResult checkActivityUse(String activityTime) {
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        String redParperId=activityMapper.checkActivityUse(phoneName, TimeUtil.StringToLocalDateTime(activityTime));
        if(redParperId!=null){
            return ActionResult.success("已参加",redParperId);
        }
        return ActionResult.success("未参加");
    }

    @Override
    public ActionResult getAllactivityType() {
        List<String> list=activityMapper.getAllactivityType();
        return ActionResult.success("获取成功",list);
    }

    @Override
    public ActionResult getActivityOrder(String redParperId) {

        Map<String,Object> res=activityMapper.getActivityOrder(redParperId);

        return ActionResult.success("成功",res);
    }

    @Override
    public ActionResult createActivityBoxes(String activityTime, String shopLabel) {
        List<Map<String,Object>> list=shopMapper.getShopByShopLabelJoinRedParper(shopLabel);

        List<Map<String,Object>> res=new ArrayList<>();
        Random random=new Random();
        System.out.println(list);
        for(int i=0;i<9;i++){
            if(list.size()>0){
                Map<String,Object> obj=new HashMap<>(list.get(random.nextInt(list.size())));

                obj.put("chooseIndex",i);

                res.add(obj);
            }

        }

        return ActionResult.success("获取成功",res);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActionResult createActivityRedparper(Integer shopId, String activityTime, String redShopParperId, String shopLabel, Integer chooseIndex) {

        LocalDateTime actTime=TimeUtil.StringToLocalDateTime(activityTime);
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        Random r=new Random();
        RedParper redParper=new RedParper();
        String redParperId=redShopParperId+"_"+TimeUtil.LocalDateTimeToSecond(TimeUtil.StringToLocalDateTime(activityTime))+phoneName;
        redParper.setRedParperId(redParperId);
        redParper.setRedShopParperId(redShopParperId);
        redParper.setPhoneName(phoneName);
        redParper.setShopId(shopId);
        redParper.setStartTime(actTime);
        redParper.setEndTime(actTime.plusHours(4));
        redParper.setGetTime(LocalDateTime.now());
        redParper.setFullReduction(0);
        redParper.setReduction((r.nextInt(3)+1)*100);
        Integer res1=redParperMapper.saveCreateRedParper(redParper);
        Activity activity=new Activity();
        activity.setActivityId("ac_"+redParperId);
        activity.setActivityTime(TimeUtil.StringToLocalDateTime(activityTime));
        activity.setGetTime(LocalDateTime.now());
        activity.setPhoneName(phoneName);
        activity.setRedParperId(redParperId);
        activity.setShopLabel(shopLabel);
        activity.setChooseIndex(chooseIndex);
        Integer res2=activityMapper.saveActivityRecord(activity);
        if(res1==1&&res2==1)
            return ActionResult.success("生成成功",redParper.getRedParperId());
        else
            return ActionResult.error("获取失败");
    }

    @Override
    public ActionResult createShopActivityRedparper(Integer shopId) {
        RedShopParper redShopParper=new RedShopParper();
        redShopParper.setRedShopParperId("activity_"+shopId+System.currentTimeMillis());
        redShopParper.setShopId(shopId);
        redShopParper.setFullReduction(0);
        redShopParper.setEnable(1);
        int res=redParperMapper.addShopRedParper(redShopParper);
        return res==1?ActionResult.success("加入成功"):ActionResult.error("加入失败");
    }

    @Override
    public ActionResult getActivityByShop(Integer shopId) {

        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        LocalDateTime now=LocalDateTime.now();
        Integer reduction=activityMapper.getActivityByShop(phoneName,shopId,now);
        return ActionResult.success("获取完毕",reduction);
    }
}
